package com.erlinyou.im.web.controller.push;

import java.io.InputStream;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;

import com.erlinyou.im.web.controller.push.globalSetting;
import com.erlinyou.im.web.controller.base.JsonUtil;
import com.erlinyou.im.web.controller.push.IApnsService;
import com.erlinyou.im.web.controller.push.service.ApnsServiceImpl;
import com.erlinyou.im.web.controller.push.model.ApnsConfig;
import com.erlinyou.im.web.controller.push.model.Feedback;
import com.erlinyou.im.web.controller.push.model.Payload;

public class PushMsgAPNS4j {
	private static IApnsService voip_apnsService;
	private static IApnsService apnsService;
	private static IApnsService map_apnsService;
	private static IApnsService booking_apnsService;
	private static IApnsService trip_apnsService;
	
//	private static IApnsService getTripApnsService() {
//		if (map_apnsService == null)
//		{
//			ApnsConfig config = new ApnsConfig();
//			InputStream is = PushMsgAPNS4j.class.getClassLoader().getResourceAsStream(globalSetting.trip_sCAFile);
//			config.setKeyStore(is);
//			config.setDevEnv(globalSetting.trip_sCAEnviorment.equalsIgnoreCase("development"));
//			config.setPassword(globalSetting.trip_sCAPass);
//			config.setPoolSize(3);
//			config.setName("trip_");
//			trip_apnsService = ApnsServiceImpl.createInstance(config);
//		}
//		return trip_apnsService;
//	}
//	
//	private static IApnsService getBookingApnsService() {
//		if (map_apnsService == null)
//		{
//			ApnsConfig config = new ApnsConfig();
//			InputStream is = PushMsgAPNS4j.class.getClassLoader().getResourceAsStream(globalSetting.booking_sCAFile);
//			config.setKeyStore(is);
//			config.setDevEnv(globalSetting.booking_sCAEnviorment.equalsIgnoreCase("development"));
//			config.setPassword(globalSetting.booking_sCAPass);
//			config.setPoolSize(3);
//			config.setName("booking_");
//			booking_apnsService = ApnsServiceImpl.createInstance(config);
//		}
//		return booking_apnsService;
//	}
//	
//	private static IApnsService getMapApnsService() {
//		if (map_apnsService == null)
//		{
//			ApnsConfig config = new ApnsConfig();
//			InputStream is = PushMsgAPNS4j.class.getClassLoader().getResourceAsStream(globalSetting.map_sCAFile);
//			config.setKeyStore(is);
//			config.setDevEnv(globalSetting.map_sCAEnviorment.equalsIgnoreCase("development"));
//			config.setPassword(globalSetting.map_sCAPass);
//			config.setPoolSize(3);
//			config.setName("map_");
//			map_apnsService = ApnsServiceImpl.createInstance(config);
//		}
//		return map_apnsService;
//	}
	
	private static IApnsService getApnsService() {
		if (apnsService == null)
		{
			ApnsConfig config = new ApnsConfig();
			InputStream is = PushMsgAPNS4j.class.getClassLoader().getResourceAsStream(globalSetting.sCAFile);
			config.setKeyStore(is);
			config.setDevEnv(globalSetting.sCAEnviorment.equalsIgnoreCase("development"));
			config.setPassword(globalSetting.sCAPass);
			config.setPoolSize(3);
			apnsService = ApnsServiceImpl.createInstance(config);
		}
		return apnsService;
	}
	
	private static IApnsService getApnsVoipService() {
		if (voip_apnsService == null)
		{
			ApnsConfig config = new ApnsConfig();
			InputStream is = PushMsgAPNS4j.class.getClassLoader().getResourceAsStream(globalSetting.voip_sCAFile);
			config.setKeyStore(is);
			config.setDevEnv(globalSetting.voip_sCAEnviorment.equalsIgnoreCase("development"));
			config.setPassword(globalSetting.voip_sCAPass);
			config.setPoolSize(3);
			config.setName("voip_");
			voip_apnsService = ApnsServiceImpl.createInstance(config);
		}
		return voip_apnsService;
	}
	
	public static void pushMsg(String token,String msgType,JSONObject obj,String[] args){
		pushMsg(token,msgType,obj,args,null,1);
	}
	
	public static void pushMsg(String token,String msgType,JSONObject obj,String[] args,String resource,int version)
	{
		IApnsService service = null;
		if("media msg".equals(msgType)){
			service = getApnsVoipService();
		}else{
//			if(StringUtils.isEmpty(resource)){
//				service = getApnsService();
//			}else if(resource.equals(Constants.RESOURCE_MAP)){
//				service = getMapApnsService();
//			}else if(resource.equals(Constants.RESOURCE_BOOKING)){
//				service = getBookingApnsService();
//			}else if(resource.equals(Constants.RESOURCE_TRIP)){
//				service = getTripApnsService();
//			}
			service = getApnsService();
		}
		
		Payload payLoad = new Payload();
		
		payLoad.addParam("content", JsonUtil.ObjectToJson(obj));
	//	payLoad.addParam("content", obj.getString("content"));
		
		if("media msg".equals(msgType) && version == 1){//如果是老版本
			//media
			payLoad.setContentAvailable(1);
			payLoad.setAlert(null);
			payLoad.setAlertLocKey(null);
			payLoad.setAlertLocArgs(null);
			payLoad.setSound(null);
		}else{
			payLoad.setAlertLocKey(msgType);
			payLoad.setAlertLocArgs(args);
			payLoad.setSound("default");
		}
		
		
		service.sendNotification(token, payLoad);
		List<Feedback> list = service.getFeedbacks();
		if (list != null && list.size() > 0) {
			for (Feedback feedback : list) {
				System.out.println(feedback.getDate() + " " + feedback.getToken());
			}
		}
	}
}
